﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Drawing;

namespace MapCenterSystem.DefineType
{    
    public class Line
    {
        // Doan thang MN co phuong tinh Ax + By + C = 0 va 2 dau mut M, N.
        private float _a;
        private float _b;
        private float _c;
        private PointF _start;
        private PointF _end;

        

        public float A
        {
            get { return _a; }
            set { _a = value; }
        }

        public float B
        {
            get { return _b; }
            set { _b = value; }
        }      

        public float C
        {
            get { return _c; }
            set { _c = value; }
        }

        public PointF Start
        {
            get { return _start; }
            set { _start = value; }
        }


        public PointF End
        {
            get { return _end; }
            set { _end = value; }
        }

        public Line(PointF p1, PointF p2)
        {            
            if (p1.X == p2.X)
            {
                //x + c = 0
                _a = 1;
                _b = 0;
                _c = - p1.X;
            }
            else if (p1.Y == p2.Y)
            {
                //y + c = 0
                _a = 0;
                _b = 1;
                _c = - p1.Y;
            }
            else
            {
                //b = -1 giai he => y = ax + c
                //a.x1 + c = y1;
                //a.x2 + c = y2;

                _b = -1;
                _a = (p2.Y - p1.Y) / (p2.X - p1.X);
                _c = p1.Y - p1.X * _a;
            }

            _start = p1;
            _end = p2;
        }

        public Line()
        {
            _a = 0;
            _b = 0;
            _c = 0;
            _start = PointF.Empty;
            _end = PointF.Empty;
        }

        public float Fxy(PointF p)
        {
            return A * p.X + B * p.Y + C;
        }

        public static PointF Intersect(Line m, Line n)
        {   
            /*Giai he de tim giao diem cua 2 DUONG THANG:*/
            //Ax + By = -C;
            //A'x + B'y = -C';

            PointF intersect = PointF.Empty;
            float D = m.A * n.B - m.B * n.A;
            float Dx = (-m.C) * n.B - m.B * (-n.C);
            float Dy = m.A * (-n.C) - (-m.C) * n.A;

            if (D != 0)
            {
                intersect.X = Dx / D;
                intersect.Y = Dy / D;

                /* Kiem tra 2 DOAN THANG co cat nhau hay ko? */
                if (!((intersect.X - m.Start.X) * (intersect.X - m.End.X) <= 0
                    && (intersect.Y - m.Start.Y) * (intersect.Y - m.End.Y) <= 0))
                {
                    intersect = PointF.Empty;
                }
            }

            return intersect;
        }

        //Giao diem cua hinh chu nhat ABCD voi duong thang MN
        public static List<PointF> IntersectWithRectangle(Line AB, Line BC, Line CD, Line DA, Line MN)
        {            
            List<PointF> lstResult = new List<PointF>();
            PointF temp = PointF.Empty;

            //Thu tu xet la AB, DC, BC, DA (xet 2 canh doi nhau truoc)
            temp = Line.Intersect(MN, AB);
            if (temp != PointF.Empty)
                lstResult.Add(temp);

            temp = Line.Intersect(MN, CD);
            if (temp != PointF.Empty)
                lstResult.Add(temp);
            if(lstResult.Count == 2)
                return lstResult;

            temp = Line.Intersect(MN, BC);
            if (temp != PointF.Empty)
                lstResult.Add(temp);
            if (lstResult.Count == 2)
                return lstResult;

            temp = Line.Intersect(MN, DA);
            if (temp != PointF.Empty)
                lstResult.Add(temp);

            return lstResult;
        }
    }
}
